#region Directives
using System;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Collections.Generic;

using VisualStudioSensor;
using TeamFoundationServerSensor;
#endregion

/// <summary>
/// Web Service Endpoint for the <seealso href="NodesDeletedEvent"/>
/// </summary>
/// <remarks>
/// To Register this Notification Endpoint you will need to use the <seealso href="http://blogs.msdn.com/psheill/archive/2006/02/01/522386.aspx">BisSubscribe.exe</seealso> tool. 
/// For more information about Team Foundation Server Notifications <seealso href="http://blogs.msdn.com/buckh/archive/2006/04/10/event_filtering.aspx"/>
/// </remarks>
[WebService(Namespace = "http://schemas.conchango.com/TeamFoundation/2005/06/Services/Notification/Extensibility", Description = "")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class NodesDeletedEndpoint : TeamFoundationServerSensor.EndpointBase {

    /// <summary>
    /// Endpoint required to accept incoming <see href="NodesDeletedEvent"/> notifications
    /// </summary>
    /// <param name="eventXml">Serialized version of the <see href="NodesDeletedEvent"/></param>
    /// <param name="tfsIdentityXml">Serialized version of <see href="TFSIdentity"/></param>
    /// <returns>True if method succeeded in reading data. False otherwise.</returns>
    [WebMethod]
    [SoapDocumentMethod("http://schemas.microsoft.com/TeamFoundation/2005/06/Services/Notification/03/Notify", RequestNamespace = "http://schemas.microsoft.com/TeamFoundation/2005/06/Services/Notification/03")]
    public override Boolean Notify(string eventXml, string tfsIdentityXml) {
        NodesDeletedEvent nodesDeletedEvent = this.CreateInstance<NodesDeletedEvent>(eventXml);
        TFSIdentity tfsIdentity = this.CreateInstance<TFSIdentity>(tfsIdentityXml);

        Boolean success = true;

        // Attempt to send data for each node deleted. 
        foreach (DeletedNode node in nodesDeletedEvent.NodesDeleted) {
            try {

                // Common data collected.
                Dictionary<string, string> pMap_DevEvent = new Dictionary<string, string>();
                pMap_DevEvent.Add("SubType", "Node Deleted");

                // Extra Useful information to send
                pMap_DevEvent.Add("Project", nodesDeletedEvent.ProjectUri);
                pMap_DevEvent.Add("Time", nodesDeletedEvent.DeletedTime);
                pMap_DevEvent.Add("User", nodesDeletedEvent.DeletedUser);
                pMap_DevEvent.Add("NodeURI", node.Uri);

                // Determine what type of data is in the artifact
                SensorShellProxy.TheInstance.SendDevEventData("StructureChange",
                    node.Uri,
                    pMap_DevEvent);
            }
            catch (Exception e) {
                Logger.TheInstance.Log("Error sending node deleted data. Data will be lost. " + e, LogLevel.Error);
                success = false;
            }
        }

        return success;
    }
}